package class202205.demo15.demo11.linkedtistdemo;

import com.sun.javafx.scene.input.InputEventUtils;

import java.util.Stack;

public class MyLinkedList {
    static class ListNode{
        public int val;//数值阈
        public ListNode prev;//前驱
        public ListNode next;//后继
        //直需要实例化数值
        public ListNode(int val){
            this.val=val;
        }

    }
    //有头有尾
    public ListNode head;//标记头
    public ListNode last;//标记尾巴
    //打印
    public void display() {
        ListNode cur = head;
        while (cur != null){
            System.out.print(cur.val+" ");
            cur =cur.next;
        }
        System.out.println();
    }
    //头插法
    public void addFirst(int data) {
        ListNode node = new ListNode(data);//先有这个节点
        if (head == null){//链表为空
            head=node;
            last=node;
        }else {//链表不为空
            node.next=head;
            head.prev=node;
            head=node;
        }
    }
    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);//先有这个节点
        if (head == null){
            head=node;
            last=node;
        }else {
            last.next=node;
            node.prev=node;
            last=node;
        }
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index, int data) {
        if (index<0 || index>size()){
            System.out.println("index不合法！");
            return;
        }
        if (index==0){
            addFirst(data);
            return;
        }
        if (index==size()){
            addLast(data);
            return;
        }
        ListNode cur = searchIndex(index);//调用下面cur拿到了index下标结点的地址
        ListNode node = new ListNode(data);//先有这个节点

        node.next=cur;
        cur.prev.next=node;
        node.prev=cur.prev;
        cur.prev=node;
    }

    /**
     * 找到index节点的下标
     * @param index
     * @return
     */
    private ListNode searchIndex(int index){
        ListNode cur = head;
        while (index!=0){
            cur=cur.next;
            index--;
        }
        return cur;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        int count = 0;
        ListNode cur = head;
        while (cur!=null){
            if (cur.val==key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key) {

    }
    //删除所有值为key的节点
    public void removeAllKey(int key) {
        ListNode cur = head;
        while (cur != null) {
            //判断删除的头节点
            if (cur.val == key) {
                if (cur == head) {
                    head = head.next;
                    if (head != null) {
                        head.prev = null;
                    }
                } else {
                    //中间和尾巴的情况
                    cur.prev.next = cur.next;
                    if (cur.next != null){
                        cur.next.prev = cur.prev;
                    }else {
                        last = last.prev;
                    }
                }
                //return;//删除一个
                cur = cur.next;//删除所有key
            } else {
                cur = cur.next;
            }
        }
    }
    //得到单链表的长度
    public int size() {
        int count = 0;
        ListNode cur = head;
        while (cur!=null){
            count++;
            cur = cur.next;
        }
       return count;
    }
    public void clear() {
        ListNode cur = head;
        while (cur != null){
            ListNode curNext = cur.next;
            //cur.val = null;//引用类型这里为空
            cur.prev = null;
            cur.next = null;
            cur = cur.next;
        }
        head = null;
        last = null;
    }

    /**
     * 将递归转化为循环  逆序
     * @param head
     */
    /*public void disPlay(ListNode head){
        if (head == null){
            return;
        }
        if (head.next == null){
            System.out.println(head.val);
            return;
        }
        disPlay(head.next);//正序的话  两行换位置
        System.out.println(head.val);
    }*/

    /**
     * 改为循环写
     */
    public void disPlay(){
        if (head == null){
            return;
        }
        Stack<ListNode> stack = new Stack<>();//栈
        ListNode cur = head;
        while (cur != null){
            stack.push(cur);
            cur = cur.next;
        }
        while (!stack.empty()){
            ListNode p = stack.pop();
            System.out.println(p.val);
        }
    }
}
